local skill = fk.CreateSkill{
  name = "tea__woluan",
  tags = { Skill.Compulsory }
}
---@type TrigSkelSpec<TrigFunc>
local woluan = {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skill.name) and target == player and player.phase == Player.Start
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, skill.name, "special")
    local all_choices = {"woluan_drawcards", "woluan_discard", "woluan_addition", "woluan_attackrange"}
    local choices = room:askToChoices(player, {
      choices = all_choices,
      min_num = 1,
      max_num = 4,
      skill_name = skill.name,
      cancelable = false,
    })
    local num = 4 - #choices
    if num > 0 then
      room:sendLog{
        type = "#TypeWoluanLog",
        from = player.id,
        arg = table.concat(table.map(choices, function(v)
          return Fk:translate(v)
        end), "、"),
        arg2 = tostring(num)
      }
      if num < 4 then
        room:notifySkillInvoked(player, skill.name, "special")
        player:broadcastSkillInvoke(skill.name, num)
      end
      if table.contains(choices, "woluan_drawcards") then
        player:drawCards(num, skill.name)
      end
      if table.contains(choices, "woluan_discard") then
        local targets = table.filter(room.alive_players, function (p) return not p:isNude() end)
        if #targets >= num then 
          local tos = room:askToChoosePlayers(player, {
            targets = targets,
            min_num = 1,
            max_num = num,
            skill_name = skill.name,
            prompt = "#tea__woluan-choose:::" .. tostring(num),
          })
          if #tos > 0 then
            room:sortByAction(tos)
            for _, to in ipairs(tos) do
              if player.dead then break end
              if not to:isNude() then
                local id = room:askToChooseCard(player, {
                  target = to,
                  skill_name = skill.name,
                  flag = "he",
                })
                room:throwCard({id}, self.name, to, player)
              end
            end
          end
        end
      end
      if table.contains(choices, "woluan_addition") then
        room:setPlayerMark(player, "tea__wuluan_addition-turn", num - 1)
      end
      if table.contains(choices, "woluan_attackrange") then
        room:setPlayerMark(player, "tea__wuluan_attackrange-turn", num)
      end
    end
  end,
}
---@type TrigSkelSpec<TrigFunc>
local woluan_re = {
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(skill.name) and player:getMark("tea__wuluan_addition-turn") > 0 then
      local logic = player.room.logic
      local suc = {}
      logic:getEventsOfScope(GameEvent.UseCard, 999, function (e)
        local use = e.data
        if use.from == player then
          table.insertIfNeed(suc, use)
        end
        return false
      end, Player.HistoryTurn)
      return #suc == 1
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    data.additionalEffect = (data.additionalEffect or 0) + player:getMark("tea__wuluan_addition-turn")
    room:setPlayerMark(player, "tea__wuluan_addition-turn", 0)
  end,
}
---@type AttackRangeSpec
local woluan_attackrange = {
  correct_func = function (self, from, to)
    return from:getMark("tea__wuluan_attackrange-turn")
  end,
}
skill:addEffect(fk.EventPhaseStart, woluan)
skill:addEffect(fk.CardUsing, woluan_re)
skill:addEffect("atkrange", woluan_attackrange)
return skill